<?php

namespace app\api\service;


use addons\xyb\library\Wxapp;
use app\api\model\xyb\Order;
use think\Log;

class TemplateService
{

    public static $template = [
        '2tladgAsJ-4OSfEP1RQC91YJ1smzKw0eFRjtP-eByU4',//取货成功通知
        '_SSVo_y1MZTIoWLzT8iNb5PEeKVZvLYK-LE2jibCwMA',//商品送达提醒
        'bV92XoZjXlX-SU1KgAtIp6q_l-jSAegJYTHJY6up-Xo',//订单状态变化通知
    ];
    public static $status_change_notice = 'bV92XoZjXlX-SU1KgAtIp6q_l-jSAegJYTHJY6up-Xo';
    /*新订单*/
    public static function sendTemplateNewOrder($agent_id, $order_id)
    {
        $field = 'id,order_no,user_id,receive_id,total_fee,type,pay_time';
        if(!$orders = Order::field($field)->where('id', $order_id)->find()){
            return false;
        }
        $type_text = Order::getTypeNameTextName($orders->type);
        if(!$user_open_id = UsersService::getUsersOpenId($orders->user_id)){
            return false;
        }
        $params = [
            'character_string5' => ['value' => $orders->order_no],
            'phrase6'           => ['value' => '支付成功'],
            'thing7'           => ['value' => $type_text ],
            'time8'           => ['value' => date('Y-m-d H:i:s', $orders->pay_time)],
            'thing1'           => ['value' => '点击进入小程序查看详情！'],
        ];
        self::sendOrdersTemplate(self::$status_change_notice, $user_open_id, $order_id, $params);
        return true;
    }

    /*接单*/
    public static function sendTemplateReceiving($agent_id, $order_id)
    {
        $field = 'id,order_no,user_id,receive_id,total_fee,type,receive_time';
        if(!$orders = Order::field($field)->where('id', $order_id)->find()){
            return false;
        }
        $type_text = Order::getTypeNameTextName($orders->type);
        if(!$user_open_id = UsersService::getUsersOpenId($orders->user_id)){
            return false;
        }
        $params = [
            'character_string5' => ['value' => $orders->order_no],
            'phrase6'           => ['value' => '已接单'],
            'thing7'            => ['value' => $type_text ],
            'time8'             => ['value' => date('Y-m-d H:i:s', $orders->receive_time)],
            'thing1'            => ['value' => '点击进入小程序查看详情！'],
        ];
        self::sendOrdersTemplate(self::$status_change_notice, $user_open_id, $order_id, $params);
        if(!$receive_open_id = UsersService::getUsersOpenId($orders->receive_id)){
            return false;
        }
        self::sendOrdersTemplate(self::$status_change_notice, $receive_open_id, $order_id, $params);
        return true;

    }

    /*取件*/
    public static function sendTemplatePackage($agent_id, $order_id)
    {
        $field = 'id,order_no,user_id,receive_id,total_fee,type,package_time';
        if(!$orders = Order::field($field)->where('id', $order_id)->find()){
            return false;
        }
        $type_text = Order::getTypeNameTextName($orders->type);
        if(!$user_open_id = UsersService::getUsersOpenId($orders->user_id)){
            return false;
        }
        $params = [
            'character_string5' => ['value' => $orders->order_no],
            'phrase6'           => ['value' => '已取件'],
            'thing7'            => ['value' => $type_text ],
            'time8'             => ['value' => date('Y-m-d H:i:s', $orders->package_time)],
            'thing1'            => ['value' => '点击进入小程序查看详情！'],
        ];
        self::sendOrdersTemplate(self::$status_change_notice, $user_open_id, $order_id, $params);
        return true;
    }

    /*送达*/
    public static function sendTemplateDelivery($agent_id, $order_id)
    {
        $field = 'id,order_no,user_id,receive_id,total_fee,type,send_time';
        if(!$orders = Order::field($field)->where('id', $order_id)->find()){
            return false;
        }
        $type_text = Order::getTypeNameTextName($orders->type);
        if(!$user_open_id = UsersService::getUsersOpenId($orders->user_id)){
            return false;
        }
        $params = [
            'character_string5' => ['value' => $orders->order_no],
            'phrase6'           => ['value' => '已送达'],
            'thing7'            => ['value' => $type_text ],
            'time8'             => ['value' => date('Y-m-d H:i:s', $orders->send_time)],
            'thing1'            => ['value' => '点击进入小程序查看详情！'],
        ];
        self::sendOrdersTemplate(self::$status_change_notice, $user_open_id,$order_id, $params);
        return true;

    }

    /*完成*/
    public static function sendTemplateComplete($agent_id, $order_id)
    {
//        $field = 'id,order_no,user_id,receive_id,total_fee,type,complete_time';
//        if(!$orders = Order::field($field)->where('id', $order_id)->find()){
//            return false;
//        }
//        $type_text = Order::getTypeNameTextName($orders->type);
//        if(!$user_open_id = UsersService::getUsersOpenId($orders->user_id)){
//            return false;
//        }
//        $params = [
//            'character_string5' => ['value' => $orders->order_no],
//            'phrase6'           => ['value' => '已送达'],
//            'thing7'            => ['value' => $type_text ],
//            'time8'             => ['value' => date('Y-m-d H:i:s', $orders->send_time)],
//            'thing1'            => ['value' => '点击进入小程序查看详情！'],
//        ];
//        self::sendOrdersTemplate(self::$status_change_notice, $user_open_id, $params);
        return true;

    }

    /*骑手审核*/
    public static function sendTemplateRiderExamine($agent_id, $users_id)
    {
        return true;

    }


    public static function sendOrdersTemplate($template_id, $open_id, $order_id, $params)
    {
        $res = Wxapp ::subscribeMessage(
            $template_id,
            $open_id,
            '/pagesD/order/detail?id='.$order_id,
            $params);
        Log::info($res);
        return $res;

    }



    public static function sendUsersTemplate($template_id, $open_id, $params)
    {
        $res = Wxapp ::subscribeMessage(
            $template_id, $open_id, '/pagesD/order/detail?id=43',
            [
                'character_string1' => ['value' => 'LLZG20180428140158Sm3QecVk0io0'],
                'time3' => ['value' => '2020-11-22 10:12:12'],
                'thing6' => ['value' => '商品下单后将在24小时内发货'],
            ]
        );
        var_dump($res);

    }


}